package com.afollestad.commons.simplelist;

import com.afollestad.commons.ResourceTable;
import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.internal.MDAdapter;
import com.afollestad.materialdialogs.utils.UiUtil;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * See the sample project to understand how this is used. Mimics the Simple List dialog style
 * displayed on Google's guidelines site:
 * https://www.google.com/design/spec/components/dialogs.html#dialogs-simple-dialogs
 *
 * @author Aidan Follestad (afollestad)
 */
public class MaterialSimpleListAdapter extends BaseItemProvider implements MDAdapter {
    private static final String TAG = MaterialSimpleListAdapter.class.getSimpleName();
    private MaterialDialog mDialog;
    private List<MaterialSimpleListItem> mItems;
    private Callback mCallback;
    private Context mContext;

    public MaterialSimpleListAdapter(Context context, Callback callback) {
        mContext = context;
        mItems = new ArrayList<>(4);
        this.mCallback = callback;
    }

    public void add(MaterialSimpleListItem item) {
        mItems.add(item);
        notifyDataSetItemInserted(mItems.size() - 1);
    }

    public void clear() {
        mItems.clear();
        notifyDataChanged();
    }

    @Override
    public void setDialog(MaterialDialog dialog) {
        this.mDialog = dialog;
    }

    public MaterialSimpleListItem getItem(int index) {
        return mItems.get(index);
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int index, Component component, ComponentContainer componentContainer) {
        if (component == null) {
            component = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_md_simplelist_item, componentContainer, false);
        }
        onPrepareView(index, component);
        return component;
    }

    private void onPrepareView(int index, Component component) {
        if (mDialog != null) {
            final MaterialSimpleListItem item = mItems.get(index);
            Image icon = UiUtil.getComponent(component, ResourceTable.Id_icon);
            if (null != icon) {
                if (item.getIcon() != null) {
                    icon.setImageElement(item.getIcon());
                    icon.setPadding(
                            item.getIconPadding(),
                            item.getIconPadding(),
                            item.getIconPadding(),
                            item.getIconPadding());
                } else {
                    icon.setVisibility(Component.HIDE);
                }
            }
            Text title = UiUtil.getComponent(component, ResourceTable.Id_title);
            if (title != null) {
                title.setTextColor(new Color(mDialog.getBuilder().getItemColor()));
                title.setText(item.getContent().toString());
                mDialog.setTypeface(title, mDialog.getBuilder().getRegularFont());
            }
        }
        component.setClickedListener(view -> {
            onItemClicked(view, index);
        });
    }

    private void onItemClicked(Component component, int index) {
        if (mCallback != null) {
            mCallback.onMaterialListItemSelected(mDialog, index, getItem(index));
        }
    }

    public interface Callback {
        void onMaterialListItemSelected(MaterialDialog dialog, int index, MaterialSimpleListItem item);
    }
}
